﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
#if !XBOX
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace Brain
{
    public class SoundSystemEntry : ITouchMenuEntry, IUpdateable, IDrawable
    {
        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        public Rectangle SelectionArea
        {
            get { return SoundSprite.BoundsRectangle; }
        }

        public void Clicked()
        {
            SoundSystem.SoundEnabled = !SoundSystem.SoundEnabled;

            if (SoundSystem.SoundEnabled)
            {
                if (OnUserEnabledSound != null)
                {
                    OnUserEnabledSound();
                }
            }
            else
            {
                if (OnUserDisabledSound != null)
                {
                    OnUserDisabledSound();
                }
            }
        }

        /// <summary>
        /// The Sprite to show when the Sound is enabled
        /// </summary>
        public Sprite SoundSprite;

        /// <summary>
        /// The Sprite to show when the Sound is not enabled
        /// </summary>
        public Sprite NoSoundSprite;

        /// <summary>
        /// Manages Sound on Touch devices. 
        /// NEEDS GestureType of Type TAP to change
        /// Sound State!
        /// </summary>
        /// <param name="sound"></param>
        /// <param name="noSound"></param>
        public SoundSystemEntry(Sprite sound, Sprite noSound, TouchMenuSystem system)
        {
            this.SoundSprite = sound;
            this.NoSoundSprite = noSound;

            system.AddEntry(this);

            ShouldUpdate = true;
            ShouldDraw = true;
        }
        /// <summary>
        /// Manages Sound on Touch devices. 
        /// NEEDS GestureType of Type TAP to change
        /// Sound State!
        /// </summary>
        public SoundSystemEntry(string texture, Vector2 position)
        {
            this.SoundSprite = new Sprite();
            this.SoundSprite.TexturePlace = texture;
            this.SoundSprite.Position = position;

            this.NoSoundSprite = new Sprite();
            this.NoSoundSprite.TexturePlace = texture;
            this.NoSoundSprite.Position = position;

            ShouldUpdate = true;
            ShouldDraw = true;
        }
        /// <summary>
        /// Manages Sound on Touch devices. 
        /// NEEDS GestureType of Type TAP to change
        /// Sound State!
        /// </summary>
        public SoundSystemEntry(string texture, Vector2 position, Vector2 scale)
        {
            this.SoundSprite = new Sprite();
            this.SoundSprite.TexturePlace = texture;
            this.SoundSprite.Position = position;
            this.SoundSprite.UpdateBoundsRectangle();
            this.SoundSprite.BoundsRectangle = RectangleUtil.Scale(this.SoundSprite.BoundsRectangle, scale);

            this.NoSoundSprite = new Sprite();
            this.NoSoundSprite.TexturePlace = texture;
            this.NoSoundSprite.Position = position;
            this.NoSoundSprite.BoundsRectangle = this.SoundSprite.BoundsRectangle;

            ShouldUpdate = true;
            ShouldDraw = true;
        }

        public event SetVoid OnUserEnabledSound;
        public event SetVoid OnUserDisabledSound;

        public void Update()
        {
            if (SoundSprite.ShouldUpdate)
            {
                SoundSprite.Update();
            }
            if (NoSoundSprite.ShouldUpdate)
            {
                NoSoundSprite.Update();
            }

            if (SoundSystem.SoundEnabled)
            {
                SoundSprite.Rendering = SpriteRendering.Rendered;
                NoSoundSprite.Rendering = SpriteRendering.Invisible;
            }
            else
            {
                SoundSprite.Rendering = SpriteRendering.Invisible;
                NoSoundSprite.Rendering = SpriteRendering.Rendered;
            }
        }
        public void Draw()
        {
            if (SoundSprite.ShouldDraw)
            {
                SoundSprite.Draw();
            }
            if (NoSoundSprite.ShouldDraw)
            {
                NoSoundSprite.Draw();
            }
        }




    }
}
